package com.jspgou.cms.action.front;

import static com.jspgou.cms.Constants.MEMBER_SYS;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.common.page.Pagination;
import com.jspgou.common.security.annotation.Secured;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.OrderItemMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;

@Secured
@Controller
public class MyInfomationAct {
	private static final Logger log = LoggerFactory.getLogger(MyInfomationAct.class);
	/**
	 * 个人信息
	 */
	private static final String MY_INFO = " ";

	@RequestMapping(value = "/my_infomation.jspx", method = RequestMethod.GET)
	public String myInfomation(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		model.addAttribute("member", member);
		model.addAttribute("historyProductIds", "");
		Cookie[] cookie = request.getCookies();
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("shop_record")){
				String str = cookie[i].getValue();
				model.addAttribute("historyProductIds", str);
				break;
			}
		}
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				MY_INFO), request);
	}


	@RequestMapping(value = "/buyRecord*.jspx")// 购物记录（wang ze wu）会员中心的
	public String getBuyRecord(Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if (pageNo == null || pageNo < 1) {
			pageNo = 1;
		}
		Pagination pagination = orderItemMng.getPageByMember(web.getId(), 0, member
				.getId(), pageNo, 2);
		model.addAttribute("pagination", pagination);
		model.addAttribute("historyProductIds", "");
		Cookie[] cookie = request.getCookies();
		for(int i=0;i<cookie.length;i++){
			if(cookie[i].getName().equals("shop_record")){
				String str = cookie[i].getValue();
				model.addAttribute("historyProductIds", str);
				break;
			}
		}
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,
				"tpl.buyRecord"), request);
	}

	@Autowired
	private OrderItemMng orderItemMng;


}
